import request from '@/utils/request'
import type { SpuImageData } from './sku'
export interface BaseSaleAttrData {
  id: number
  name: string
}

interface SpuSaleAttrValueData {
  id?: number
  spuId?: number
  baseSaleAttrId: number | string
  saleAttrValueName: string | undefined
  saleAttrName?: string
  isChecked?: null
}

export type SpuSaleAttrData = {
  id?: number
  spuId?: number
  baseSaleAttrId: number | string
  saleAttrName: string
  spuSaleAttrValueList: SpuSaleAttrValueData[]
  isEdit?: boolean
  saleAttrValueName?: string
  saleAttrIdValueId?: string
}

export type SpuData = {
  id?: number
  spuName: string
  description: string
  category3Id: number | string
  tmId: number | string
  spuSaleAttrList: SpuSaleAttrData[]
  spuImageList: SpuImageData[]
}

type SpuPageListData = {
  records: SpuData[]
  total: number
  size: number
  current: number
  searchCount: boolean
  pages: number
}

export default {
  // 获取所有的销售属性列表
  // GET /admin/product/baseSaleAttrList
  // getBaseSaleAttrList
  getBaseSaleAttrList() {
    return request.get<any, BaseSaleAttrData[]>(
      '/admin/product/baseSaleAttrList'
    )
  },

  // DELETE /admin/product/deleteSpu/{spuId}
  // deleteSpu
  deleteSpu(spuId: number) {
    return request.delete<any, null>(`/admin/product/deleteSpu/${spuId}`)
  },

  // 根据spu的id获取spu详情
  // GET /admin/product/getSpuById/{spuId}
  // 获取spu基本信息
  getSpuById(spuId: number) {
    return request.get<any, SpuData>(`/admin/product/getSpuById/${spuId}`)
  },

  // POST /admin/product/inner/findSpuInfoBySpuIdList
  // findSpuInfoBySpuIdList  不用

  // POST /admin/product/saveSpuInfo
  // saveSpuInfo
  // POST /admin/product/updateSpuInfo
  // 更改spu信息
  saveSpuInfo(spu: SpuData) {
    return request.post<any, null>(
      `/admin/product/${spu.id ? 'update' : 'save'}SpuInfo`,
      spu
    )
  },

  // GET /admin/product/{page}/{limit}
  // index
  getPageList(page: number, limit: number, category3Id: number) {
    return request.get<any, SpuPageListData>(
      `/admin/product/${page}/${limit}?category3Id=${category3Id}`
    )
  },
}
